package net.maku.propagate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.framework.security.user.SecurityUser;
import net.maku.propagate.entity.PropagateTrainingManagementEntity;
import net.maku.propagate.query.PropagateTrainingManagementQuery;
import net.maku.propagate.vo.PropagateTrainingManagementVO;
import net.maku.propagate.dao.PropagateTrainingManagementDao;
import net.maku.propagate.service.PropagateTrainingManagementService;
import net.maku.system.service.SysOrgService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 培训管理
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-16
 */
@Service
@AllArgsConstructor
public class PropagateTrainingManagementServiceImpl extends BaseServiceImpl<PropagateTrainingManagementDao, PropagateTrainingManagementEntity> implements PropagateTrainingManagementService {

    private final SysOrgService sysOrgService;

    @Override
    public PageResult<PropagateTrainingManagementVO> page(PropagateTrainingManagementQuery query) {
        IPage<PropagateTrainingManagementEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        List<PropagateTrainingManagementVO> result = BeanUtil.copyToList(page.getRecords(), PropagateTrainingManagementVO.class);
        sysOrgService.setVisible(result);
        return new PageResult<>(result, page.getTotal());
    }

    private LambdaQueryWrapper<PropagateTrainingManagementEntity> getWrapper(PropagateTrainingManagementQuery query) {
        LambdaQueryWrapper<PropagateTrainingManagementEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(query.getTrainingName()), PropagateTrainingManagementEntity::getTrainingName, query.getTrainingName())
                .like(StringUtils.isNotEmpty(query.getTrainingCode()), PropagateTrainingManagementEntity::getTrainingCode, query.getTrainingCode())
                .eq(ObjectUtils.isNotEmpty(query.getTrainingType()), PropagateTrainingManagementEntity::getTrainingType, query.getTrainingType());
        dataScopeWrapper(wrapper);
        return wrapper.orderByDesc(PropagateTrainingManagementEntity::getCreateTime);
    }

    @Override
    public void save(PropagateTrainingManagementVO vo) {
        PropagateTrainingManagementEntity entity = BeanUtil.copyProperties(vo, PropagateTrainingManagementEntity.class);
        entity.setTrainingCode("KC" + System.currentTimeMillis());
        entity.setOrgId(SecurityUser.getUser().getOrgId());
        baseMapper.insert(entity);
    }

    @Override
    public void update(PropagateTrainingManagementVO vo) {
        PropagateTrainingManagementEntity entity = BeanUtil.copyProperties(vo, PropagateTrainingManagementEntity.class);

        updateById(entity);
    }

    @Override
    public void unmount(Long id, Integer status) {
        PropagateTrainingManagementEntity entity =new PropagateTrainingManagementEntity();
        entity.setId(id);
        entity.setStatus(status);
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}